<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    class="flex"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    :disabled="baTable.form.operate == 'view'"
                >
                <div class="corp-wrapper">
                    <FormItem 
                        :label="t('xContract.CORP')" 
                        style="flex: 1;margin-right: 16px;"
                        v-model="baTable.form.items!.CORP" 
                        prop="CORP" 
                        type="remoteSelect"
                        :input-attr="{
                            pk: 'CORP',
                            field: 'CORP',
                            'remote-url': corplib + 'index?type='+type,
                            placeholder: t('Click Select'),
                            onChange:getCurrentCorp
                        }"
                    />
                    <el-form>
                        <el-popover placement="right" :width="400" trigger="hover">
                            <template #reference>
                                <el-button :icon="Search" circle :disabled="false"/>
                            </template>
                            <el-descriptions title="公司信息" :column="1">
                                <el-descriptions-item label="公司名称">{{currentCorp!.CORP||'-'}}</el-descriptions-item>
                                <el-descriptions-item label="中文名">{{currentCorp!.CORPNAME||'-'}}</el-descriptions-item>
                                <el-descriptions-item label="英文名">{{currentCorp!.ENAME||'-'}}</el-descriptions-item>
                                <el-descriptions-item label="地址">
                                {{currentCorp!.ADDRESS||'-'}}
                                </el-descriptions-item>
                            </el-descriptions>
                        </el-popover>
                    </el-form>
                </div>

                <FormItem :label="t('xContract.CONTRACTTYPE')" 
                v-model="baTable.form.items!.CONTRACTTYPE" 
                prop="CONTRACTTYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'codename',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=contracttype'
                }" 
                />
                <FormItem :label="t('xContract.CONTRACTNO')" type="string" v-model="baTable.form.items!.CONTRACTNO" prop="CONTRACTNO" :input-attr="{ placeholder: t('Please input field', { field: t('xContract.CONTRACTNO') }) }" />
                <FormItem 
                :label="t('xContract.FQR')" 
                v-model="baTable.form.items!.FQR" 
                prop="FQR" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'nickname',
                    field: 'nickname',
                    'remote-url':authAdmin+'index',
                    placeholder: t('Click Select'),
                }"
                />
                <FormItem :label="t('xContract.STARTDATE')" type="datetime" v-model="baTable.form.items!.STARTDATE" prop="STARTDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xContract.STARTDATE') }) }" />
                <FormItem :label="t('xContract.ENDDATE')" type="datetime" v-model="baTable.form.items!.ENDDATE" prop="ENDDATE" :input-attr="{ placeholder: t('Please select field', { field: t('xContract.ENDDATE') }) }" />
                <FormItem :label="t('xContract.DEPT')" 
                    v-model="baTable.form.items!.DEPT" 
                    prop="DEPT" 
                    type="remoteSelect"
                    :input-attr="{
                        pk:'DEPTID',
                        field: 'DEPTNAME',
                        'remote-url':dEPARTMENT +'index',
                        placeholder: t('Click Select'),
                    }" />
                <FormItem :label="t('xContract.WORKTYPE')" type="string" v-model="baTable.form.items!.WORKTYPE" prop="WORKTYPE" :input-attr="{ placeholder: t('Please input field', { field: t('xContract.WORKTYPE') }) }" />

                <el-form-item label="有效性" prop="ACTIVE" >
                    <el-switch
                        v-model="baTable.form.items!.ACTIVE"
                        style="--el-switch-on-color: #13ce66;"
                        active-value="1"
                        inactive-value="0"
                    />
                </el-form-item>
                <!-- <FormItem :label="t('xContract.ACTIVE')" type="string" v-model="baTable.form.items!.ACTIVE" prop="ACTIVE" :input-attr="{ placeholder: t('Please input field', { field: t('xContract.ACTIVE') }) }" /> -->
                <FormItem :label="t('xContract.MDTIME')" type="string" v-model="baTable.form.items!.MDTIME" prop="" :input-attr="{ placeholder: t('Please select field', { field: t('xContract.MDTIME') }) }" />
                <FormItem
                    :label="t('xContract.OPNAME')"
                    type="string"
                    v-model="baTable.form.items!.OPNAME"
                    prop="OPNAME"
                    :input-attr="{ placeholder: '当前登录用户', disabled: true }"
                />
                <FormItem
                    :label="t('xContract.OPDATETIME')"
                    type="date"
                    v-model="baTable.form.items!.OPDATETIME"
                    prop="OPDATETIME"
                    :input-attr="{ placeholder: '当前日期', disabled: true }"
                />
                
                <!-- <FormItem :label="t('xContract.OPNAME')" type="string" v-model="baTable.form.items!.OPNAME" prop="OPNAME" :input-attr="{ placeholder: t('Please input field', { field: t('xContract.OPNAME') }) }" /> -->
                <!-- <FormItem :label="t('xContract.OPDATETIME')" type="datetime" v-model="baTable.form.items!.OPDATETIME" prop="OPDATETIME" :input-attr="{ placeholder: t('Please select field', { field: t('xContract.OPDATETIME') }) }" /> -->
                <!-- <FormItem :label="t('xContract.OPDEPT')" type="string" v-model="baTable.form.items!.OPDEPT" prop="OPDEPT" :input-attr="{ placeholder: t('Please input field', { field: t('xContract.OPDEPT') }) }" /> -->
                <FormItem :label="t('xContract.FILEPATH')" type="file" style="width:100%" v-model="baTable.form.items!.FILEPATH" prop="FILEPATH" :input-attr="{ placeholder: t('Please input field', { field: t('xContract.FILEPATH') }) }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSave" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, watchEffect } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { dEPARTMENT , corplib , codelib , authAdmin} from '/@/api/controllerUrls'
import { useRoute } from 'vue-router'
import { getSelectData } from '/@/api/common'
import {Search} from '@element-plus/icons-vue'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    CORP: [buildValidatorData({name: 'required', title: t('xContract.CORP')})],
    STARTDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xContract.STARTDATE') })})],
    ENDDATE: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xContract.ENDDATE') })})],
    MDTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xContract.MDTIME') })})],
    OPDATETIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xContract.OPDATETIME') })})],
})

const route = useRoute()
const type = route.name === 'xContract/customer'? 1:2
// baTable.form.items!.type = type

watchEffect(()=>{
    if(baTable.form.operate !== 'add'&&baTable.form.items!.CORP){
        console.log('11111',baTable.form.items!.CORP)
        getCurrentCorp(baTable.form.items!.CORP)
    }
})


interface CurrentCorp {
    CORP:string,
    ADDRESS:string,
    CORPNAME:string,
    ENAME:string
}
const currentCorp = ref<CurrentCorp|{}>({})

const getCurrentCorp = (name:string)=>{
    let url = corplib + 'index'
    getSelectData(url,name,{
        initValue:name,
    }).then(res=>{
        currentCorp.value = res.data.list[0] || {}
    })
}

const handleSave = ()=>{
    baTable.form.items!.type = type 
    baTable.onSubmit(formRef.value,()=>{
        baTable.form.operate = ''
    })
}


</script>

<style scoped lang="scss">
.corp-wrapper{
    display: flex;
    width:50%;
}
</style>
